<template>
  <div class="app-container">
    <div class="query-box">
      <el-button type="primary" plain icon="el-icon-circle-plus-outline" size="small" class="mr10" @click="handleAdd">
        新增
      </el-button>
    </div>

    <el-table
      v-loading="loading"
      :data="list"
      row-key="id"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="name" label="名称" :show-overflow-tooltip="true" width="300"></el-table-column>
      <el-table-column prop="parentName" label="上级单位"></el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" :active-value=1 :inactive-value=2 active-color="#409EFF"
                     inactive-color="#B9B9B9" @change="changeStatus(scope.$index, scope.row)"/>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" align="center" width="100"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
          >新增
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改单位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="上级单位">
              <treeSelect
                v-model="form.parentId"
                :options="orgOptions"
                :normalizer="normalizer"
                :show-count="true"
                placeholder="选择上级单位"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="单位名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入单位编码"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="角色状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio v-for="item in Object.keys(enableStatus)" :label="item">{{enableStatus[item]}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {treeOrg, saveOrg, delOrg, updateOrgStatus} from "@/api/api";
  import TreeSelect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  export default {
    name: "Auth",
    components: { TreeSelect },
    data() {
      return {
        enableStatus: window.enableStatus,
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 单位表格树数据
        list: [],
        // 单位树选项
        orgOptions: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 显示状态数据字典
        visibleOptions: [],
        // 单位状态数据字典
        statusOptions: [],
        // 查询参数
        queryParams: {
          menuName: undefined,
          visible: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [{required: true, message: "单位名称不能为空", trigger: "blur"}],
          status: [{required: true, message: "请选择状态", trigger: "blur"}],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 转换数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.id,
          label: node.name,
          children: node.children
        };
      },
      /** 查询列表 */
      getList() {
        this.loading = true;
        treeOrg().then(response => {
          console.log(response.data)
          this.orgOptions = this.list = response.data;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          parentId: undefined,
          name: undefined,
          status: undefined,
        };
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        // this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        this.reset();
        if (row != null && row.id) {
          this.form.parentId = row.id;
        } else {
          this.form.parentId = undefined;
        }
        this.form.status = "1";
        this.open = true;
        this.title = "添加单位";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.form = row;
        this.open = true;
        this.title = "修改单位【" + row.name + "】";
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            saveOrg(this.form).then(response => {
              this.msgSuccess((this.form.id ? "编辑" : "新增") + "单位成功");
              this.open = false;
              this.getList();
            });
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$confirm('是否确认删除"' + row.name + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          delOrg(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      changeStatus(index, row) {
        updateOrgStatus(row.id, row.status == 1 ? "enable" : "disable").catch(res => {
          this.list[index].status = row.status == 1 ? 2 : 1
          this.list[index].statusName = enableStatus[status]
        });
      }
    }
  };
</script>
